Home:ALL Converter>Selecting elements of n inputs in c without using arrays

Selecting elements of n inputs in c without using arrays

Ask Time:2020-02-24T00:56:12         Author:Spyromancer

Json Formatter

Given a sequence of integers as input I need to be able to select specific integers of the sequence so that I can execute various arithmetic operations between them.

For example, given this input I want to know the sum of the third and the last integer of the sequence. EDIT: in the example it would be the sum of 7 and 9

3

1

7

4

9

The condition I have to follow is that I don't use arrays so I can't give every integer an index. Also I can't know in advance how many integers there will be in the sequence so I can't create an input that determine the number of integers.

To read the inputs I thought of using this loop with a scanf condition:

 while (scanf("%d", &i) == 1);

The thing I'm stuck in is how to select the third and the last integers; if it was something like finding the sum of the odd integers I would just put a condition like this:

if (i%2 != 0)
{
   sum = sum + i;
   i++;
}

Most examples are solved using a for lopp but they either have the number of inputs declared or the inputs are just consecutive integers. Any suggestion on how can I solve this problem?

Author:Spyromancer,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364609/selecting-elements-of-n-inputs-in-c-without-using-arrays
yy